<template>
    <view>
        <view class="address-list">
            <view style="margin-bottom: 32rpx">
                <view style="color: #888; text-align: center; padding: 32rpx 0" v-if="address_list.length == 0">暂无收货地址</view>
                <view class="radio-group" v-else>
                    <view @tap="addressSelect" :data-id="item.id" v-for="(item, index) in address_list" :key="index">
                        <view class="address-item" :data-index="index">
                            <view class="userinfo flex-row">
                                <view class="flex-grow-1">收货人：{{ item.user_name }}</view>
                                <view class="flex-grow-0">{{ item.tel }}</view>
                            </view>
                            <view class="address-detail">收货地址：{{ item.address }}</view>
                        </view>

                        <view class="weui-panel__ft">
                            <view class="weui-cell weui-cell_access">
                                <label  @tap.stop="radioChange"  :data-id="item.id" class="weui-cell__bd">
                                    <radio :checked="item.is_default == '1' ? true : false" :color="color" :value="item.id"></radio>
                                    <text style="color: #999" v-if="item.is_default == '2'">设为默认</text>
                                    <text :style="'color:' + color" v-if="item.is_default == '1'">已设为默认</text>
                                </label>
                                <view @tap.stop.prevent="bianji" class="weui-cell__ft" :data-bjid="item.id">
                                    <image src="https://yunkeososyunchuc.bc8t.cn/ymq_canyino2o/static/edit.png"></image>
                                    <view class="jz">编辑</view>
                                </view>
                                <view @tap.stop.prevent="shanchu" class="weui-cell__ft" :data-scid="item.id">
                                    <image src="https://yunkeososyunchuc.bc8t.cn/ymq_canyino2o/static/delete.png"></image>
                                    <view class="jz">删除</view>
                                </view>
                            </view>
                        </view>
                    </view>
                </view>
            </view>
        </view>
        <view class="flex-row tjdz">
            <view class="flex-grow-1 px-24">
                <navigator class="btn btn-red" :style="'background:' + color" url="bjdz">手动添加</navigator>
            </view>

          <!-- #ifdef MP-WEIXIN -->
          <view class="flex-grow-1 px-24">
            <button @tap="getWechatAddress" class="btn btn-green">微信添加</button>
          </view>
          <!-- #endif -->

        </view>
    </view>
</template>

<script>
import cardlist from '@/ymq_canyino2o/dist/cardlist/index';
import gettel from '@/ymq_canyino2o/dist/gettel/index';
var qqmapsdk;
var app = getApp();
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
export default {
    components: {
        cardlist,
        gettel
    },
    data() {
        return {
            address_list: [],
            System: '',
            color: ''
        };
    },
    onLoad: function (e) {
        app.globalData.setNavigationBarColor(this);
        var that = this;
        app.globalData.util.request({
            url: 'entry/wxapp/System',
            cachetime: '0',
            success: function (e) {
                console.log(e);
                that.setData({
                    System: e.data
                });
                qqmapsdk = new QQMapWX({
                    key: e.data.map_key
                });
            }
        });
    },
    onReady: function () {},
    onShow: function () {
        this.onShowClone3389();
    },
    methods: {
        onShowClone3389: function () {
            uni.showNavigationBarLoading();
            var that = this;
            var e = uni.getStorageSync('users').id;
            app.globalData.util.request({
                url: 'entry/wxapp/MyAddress',
                cachetime: '0',

                data: {
                    user_id: e
                },

                success: function (e) {
                    console.log(e);
                    for (var t = 0; t < e.data.length; t++) {
                        e.data[t].address = e.data[t].area.join('') + e.data[t].address;
                    }
                    that.setData({
                        address_list: e.data
                    });
                }
            });
        },

        bianji: function (e) {
            var t = e.currentTarget.dataset.bjid;
            console.log(t);
            uni.navigateTo({
                url: 'bjdz?bjid=' + t,
                success: function (e) {},
                fail: function (e) {},
                complete: function (e) {}
            });
        },

        shanchu: function (e) {
            console.log(e.currentTarget.dataset.scid);
            var t = e.currentTarget.dataset.scid;
            var that = this;
            uni.showModal({
                title: '提示',
                content: '确定要删除该地址吗？',
                confirmText: '确定',
                cancelText: '取消',
                success: function (e) {
                    console.log(e);
                    if (e.confirm) {
                        app.globalData.util.request({
                            url: 'entry/wxapp/DelAdd',
                            cachetime: '0',
                            data: {
                                id: t
                            },
                            header: {
                                'content-type': 'application/json'
                            },
                            success: function (e) {
                                console.log(e);
                                if ('1' == e.data) {
                                    that.onShowClone3389({});
                                    uni.showToast({
                                        title: '删除成功',
                                        icon: 'success'
                                    });
                                }
                            }
                        });
                        console.log('用户点击确定');
                    } else {
                        console.log('用户点击取消');
                    }
                }
            });
        },
        addressSelect: function (e) {
          var current_id = e.currentTarget.dataset.id;
          uni.setStorageSync("xzdz_id", current_id);

          setTimeout(function () {
            uni.navigateBack({
              delta: 1
            });
          }, 300);
        },
        radioChange: function (e) {
            var current_id = e.currentTarget.dataset.id;
            uni.setStorageSync("xzdz_id", current_id);

            uni.getStorageSync('mydata').id;
            var that = this;
            console.log('radio发生change事件，携带value值为：', e.currentTarget.dataset.id);
            var t = e.currentTarget.dataset.id;
            app.globalData.util.request({
                url: 'entry/wxapp/AddDefault',
                cachetime: '0',
                data: {
                    id: t
                },
                success: function (e) {
                    console.log(e);
                  var current_pages = getCurrentPages();
                  console.log("current_pages",current_pages);
                    if ('1' == e.data) {
                        that.onShowClone3389({});
                        uni.showToast({
                            title: '修改成功',
                            icon: 'success',
                            duration: 1000
                        });

                    }
                    if ('2' == e.data) {
                        //none
                    }
                },
                fail: function (e) {},
                complete: function (e) {

                  //选择地址完成后,非APP的情况返回
                  // #ifndef APP-PLUS
                  if (1 < current_pages.length && 'ymq_canyino2o/pages/takeout/takeoutform' == current_pages[current_pages.length - 2].route) {
                    current_pages[current_pages.length - 2].countpsf();
                  }
                  // 选择地址后
                  if (1 < current_pages.length && 'ymq_canyino2o/pages/errand/add_order/index' == current_pages[current_pages.length - 2].route) {
                    current_pages[current_pages.length - 2].selectAddressCallback();
                  }
                  // #endif

                  //自动返回
                  setTimeout(function () {
                    uni.navigateBack({
                      delta: 1
                    });
                  }, 300);

                }
            });
        },

        getWechatAddress: function (e) {
            var n = uni.getStorageSync('users').id;
            var that = this;
            uni.chooseAddress({
                success: function (o) {
                    console.log(o);
                    if ('chooseAddress:ok' == o.errMsg) {
                        uni.showLoading();
                        qqmapsdk.geocoder({
                            address: o.provinceName + o.cityName + o.countyName + o.detailInfo,
                            success: function (e) {
                                console.log(e);
                                if ('0' == e.status) {
                                    var t = e.result.location.lat;
                                    var a = e.result.location.lng;
                                    app.globalData.util.request({
                                        url: 'entry/wxapp/AddAddress',
                                        cachetime: '0',
                                        data: {
                                            address: o.detailInfo,
                                            area: o.provinceName + ',' + o.cityName + ',' + o.countyName,
                                            user_name: o.userName,
                                            user_id: n,
                                            tel: o.telNumber,
                                            lat: t,
                                            lng: a
                                        },
                                        success: function (e) {
                                            console.log(e.data);

                                            if ('0' != e.data) {
                                                uni.showToast({
                                                    title: '保存成功',
                                                    duration: 1000
                                                });

                                                uni.setStorageSync("xzdz_id", e.data);
                                                setTimeout(function () {
                                                  uni.navigateBack({
                                                    delta: 1
                                                  });
                                                }, 300);
                                                // var t = getCurrentPages();
                                                // console.log(t);
                                                // if (1 < t.length && 'ymq_canyino2o/pages/takeout/takeoutform' == t[t.length - 2].route) {
                                                //     t[t.length - 2].countpsf();
                                                // }
                                                // setTimeout(function () {
                                                //     uni.navigateBack({
                                                //         delta: 1
                                                //     });
                                                // }, 1000);
                                                that.onShowClone3389({});
                                            }
                                        }
                                    });
                                }
                            },
                            fail: function (e) {
                                console.log(e);
                            },
                            complete: function (e) {
                                console.log(e);
                            }
                        });
                    }
                },
                fail: function () {
                    uni.getSetting({
                        success: function (e) {
                            console.log(e);
                            if (e.authSetting['scope.address']) {
                                console.log('取消');
                            } else {
                                uni.showModal({
                                    title: '提示',
                                    content: '您拒绝了获取收货地址授权，部分功能无法使用,点击确定重新获取授权。',
                                    showCancel: false,
                                    success: function (e) {
                                        if (e.confirm) {
                                            uni.openSetting({
                                                success: function (e) {
                                                    if (e.authSetting['scope.address']) {
                                                        that.getWechatAddress();
                                                    }
                                                },
                                                fail: function (e) {}
                                            });
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            });
        }
    }
};
</script>
<style>
@import './xzdz.css';
</style>
